package com.r2coding.article.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.r2coding.article.annotation.UpdateArticleView;
import com.r2coding.article.entity.ArticleEntity;
import com.r2coding.article.feign.SystemFeignService;
import com.r2coding.article.service.ArticleService;
import com.r2coding.article.to.User;
import com.r2coding.article.vo.req.QueryArticleReqVo;
import com.r2coding.article.vo.resp.ArticleListRespVo;
import com.r2coding.article.vo.resp.BaseVo;
import com.r2coding.article.vo.resp.QueryArticleRespVo;
import com.r2coding.article.vo.resp.VisitNumRespVo;
import com.r2coding.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:98k灬
 * @since:2022/04/17 12:35
 **/
@RestController
@RequestMapping("/api/article")
public class ApiArticleController {
    @Autowired
    ArticleService articleService;

    @Autowired
    SystemFeignService userFeignService;

    /**
     * 获取全部文章信息
     *
     * @return 全部文章信息
     */
    @GetMapping("/getAll")
    public Result getAll() {
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_public", 1);
        wrapper.orderByDesc("create_time");
        List<ArticleEntity> list = articleService.list(wrapper);
        return Result.ok(list);
    }

    /**
     * 获取归档文章信息
     *
     * @param req 请求
     * @return 统一响应体
     */
    @PostMapping("/getArchives")
    public Result getArchives(@RequestBody QueryArticleReqVo req) {
        PageInfo<ArticleEntity> info = articleService.getArchives(req);
        return Result.ok(info);
    }

    /**
     * 根据条件分页查询博客列表信息
     *
     * @return 博客列表信息
     */
    @PreAuthorize("hasAuthority('blog:search')")
    @PostMapping("/list")
    public Result list(@RequestBody QueryArticleReqVo vo) {
        vo.setIsPublic(0);
        ArticleListRespVo respVo = articleService.queryClientList(vo);
        return Result.ok(respVo);
    }

    @PostMapping("/clientList")
    public Result getClientlist(@RequestBody QueryArticleReqVo vo) {
        vo.setIsPublic(1);
        ArticleListRespVo respVo = articleService.queryClientList(vo);
        return Result.ok(respVo);
    }

    /**
     * 获取最新文章
     *
     * @param id 排除掉的文章
     * @return 统一响应体
     */
    @GetMapping("/newArticle/{id}")
    public Result newArticle(@PathVariable("id") Integer id) {
        List<ArticleEntity> list = articleService.newArticle(id);
        return Result.ok(list);
    }

    /**
     * 根据博客id查询详情信息
     *
     * @param id 博客id
     * @return 详情信息
     */
    @UpdateArticleView
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        QueryArticleRespVo vo = articleService.queryById(id);
        return Result.ok(vo);
    }

    /**
     * 根据文章id获取作者信息
     *
     * @param id 文章id
     * @return 作者信息
     */
    @GetMapping("/getAuthorByArticleId/{id}")
    public Result getAuthorByArticleId(@PathVariable("id") Integer id) {
        ArticleEntity byId = articleService.getById(id);
        User user = userFeignService.getById(byId.getCreaterId());
        return Result.ok(user);
    }

    /**
     * 获取文章总数、分类总数、标签总数
     *
     * @return 统一响应体
     */
    @GetMapping("/getBase")
    public Result getBase() {
        BaseVo baseVo = articleService.selectBase();
        return Result.ok(baseVo);
    }

    /**
     * 点赞或取消点赞
     *
     * @param count 点赞数
     * @param articleId 文章id
     * @return 统一响应体
     */
    @GetMapping("/thumhup/{count}/{articleId}")
    public Result thumhup(@PathVariable("count") Integer count, @PathVariable("articleId") Integer articleId) {
        articleService.thumhup(articleId, count);
        return Result.ok();
    }

    /**
     * 获取推荐文章
     *
     * @return 统一响应体
     */
    @GetMapping("/getRecommend")
    public Result getRecommend() {
        List<ArticleEntity> list = articleService.getRecommend();
        return Result.ok(list);
    }

    /**
     * 获取文章总数
     *
     * @return 文章总数
     */
    @GetMapping("/getArticleCount")
    public Integer getArticleCount() {
        return articleService.count();
    }

    /**
     * 获取一周访问量
     *
     * @return 统一响应体
     */
    @GetMapping("/getVisitNum")
    public Result getVisitNum() {
        List<VisitNumRespVo> list = articleService.getVisitNum();
        return Result.ok(list);
    }
}
